<?php

require_once("../../connection.php");

require_once("../../article_ping/to/article_ping_to.php");
require_once("../../article_ping/dao/article_ping_dao.php");


class Mysql_article_ping_dao implements Article_ping_dao{

	public function create($article_ping_to) {
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$to_article_number = $article_ping_to->get_to_article_number();
			$from_autor_login = mysql_real_escape_string($article_ping_to->get_from_autor_login());
			$comment = mysql_real_escape_string($article_ping_to->get_comment());
			$type = mysql_real_escape_string($article_ping_to->get_type());
			$date = mysql_real_escape_string($article_ping_to->get_date());	
			$state = mysql_real_escape_string($article_ping_to->get_state());				
			
			$result = mysql_query("insert into article_ping 
									(to_article_number, from_autor_login, comment, type, date, state)
									values ('$to_article_number', '$from_autor_login', '$comment', 
									'$type', '$date', '$state')", $link);			
			
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_INSERTED);
			
			Connection::singleton()->transaction_commit($link);
			
			return $article_ping_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
	public function exist($to_article_number, $from_autor_login) {

		try {
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($from_autor_login);
			
			$result = mysql_query("select * from article_ping 
									where to_article_number = '$to_article_number'
									and from_autor_login like '$from_autor_login'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);			
			return true;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	
	public function find_by_article_and_autor_login($to_article_number, $from_autor_login) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($from_autor_login);
			
			$result = mysql_query("select * from article_ping 
									where to_article_number = '$to_article_number'
									and from_autor_login like '$from_autor_login'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) 
				throw new Exception(NOT_ROW_FOUND);

			Connection::singleton()->transaction_commit($link);
			
			return mysql_fetch_assoc($result);
		}
		catch (Exception $e) {			
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);		
		}
	}
	
	
	public function update($article_ping_to) {

		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$to_article_number = $article_ping_to->get_to_article_number();
			$from_autor_login = mysql_real_escape_string($article_ping_to->get_from_autor_login());
			$state = mysql_real_escape_string($article_ping_to->get_state());
			
			$result = mysql_query("update article_ping 
									set state='$state' 
									where to_article_number = '$to_article_number'
									and from_autor_login like '$from_autor_login'", $link);
							
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_UPDATED);
			
			Connection::singleton()->transaction_commit($link);
			
			return $article_ping_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}	
		
	}
	
	
	public function remove($to_article_number, $from_autor_login) {	
		try {			
			$link = Connection::singleton()->transaction_begin();
			
			$from_autor_login = mysql_real_escape_string($from_autor_login);
			
			$result = mysql_query("delete from article_ping
									where to_article_number = '$to_article_number'
									and from_autor_login like '$from_autor_login'", $link);
									
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_DELETED);
			
			Connection::singleton()->transaction_commit($link);
					
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
}
?>